package com.test.testopengl;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;

public class MainActivity extends Activity {
	private GLSurfaceView glSurface;
	private MyOpenGlRenderer mMyOpenGlRenderer;
	private final float TOUCH_SCALE_FACTOR = 180.0f / 480;
	/**
	 * 记录上次触屏位置的坐标
	 */
	private float mPreviousX, mPreviousY;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Create an Instance with this Activity
		glSurface = new GLSurfaceView(this);
		// Set our own Renderer
		
		Intent in=getIntent();
		String path=in.getStringExtra("stlPath");
 
		
		try {
			mMyOpenGlRenderer = new MyOpenGlRenderer(path);
			glSurface.setRenderer(mMyOpenGlRenderer);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Set the GLSurface as View to this Activity
		setContentView(glSurface);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		// float xAngle =-1f;//test2
		// float yAngle =-1f;//test2
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mPreviousX = event.getX();
			mPreviousY = event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			float dx = event.getX() - mPreviousX;
			float dy = event.getY() - mPreviousY;
			mMyOpenGlRenderer.mRotatefX += dx * TOUCH_SCALE_FACTOR;
			mMyOpenGlRenderer.mRotatefY += dy * TOUCH_SCALE_FACTOR;
			mPreviousX = event.getX();
			mPreviousY = event.getY();
			break;
		case MotionEvent.ACTION_UP:
			mPreviousX = 0;
			mPreviousY = 0;
			break;
		default:
			break;
		}
		return super.onTouchEvent(event);
	}

	/**
	 * Remember to resume the glSurface
	 */
	@Override
	protected void onResume() {
		super.onResume();
		glSurface.onResume();
	}

	/**
	 * Also pause the glSurface
	 */
	@Override
	protected void onPause() {
		super.onPause();
		glSurface.onPause();
	}
}
